"ui";
const openReleasePage = require("release.js");
let webViewExpand = require("webViewExpand.js");

var color = "#009688";
ui.layout(
    <drawer id="drawer">
        <vertical>
            <appbar>
                <toolbar id="toolbar" title="京东助理"/>
                <tabs id="tabs"/>
            </appbar>
            <viewpager id="viewpager">
                <frame>
                <vertical padding="16">
                    <button text="开启农场自助" id="startFruit" style="Widget.AppCompat.Button.Colored" w="auto" />
                    <button text="开启猪场自助" id="startPig" style="Widget.AppCompat.Button.Colored" w="auto" />
                    <button text="退出" id="exit" w="auto" />
                </vertical>
                </frame>
                <frame>
                    <vertical>
                        <frame w="*" h="*">
                            <webview id="webView" layout_below="title" />
                        </frame>
                    </vertical>
                </frame>
                <frame>
                    <vertical>
                        <frame w="*" h="*">
                            <webview id="pigWebView" layout_below="title" />
                        </frame>
                    </vertical>
                </frame>
            </viewpager>
        </vertical>
        <vertical layout_gravity="left" bg="#ffffff" w="280">
            <img w="280" h="200" scaleType="fitXY" src="http://images.shejidaren.com/wp-content/uploads/2014/10/023746fki.jpg"/>
            <list id="menu">
                <horizontal bg="?selectableItemBackground" w="*">
                    <img w="50" h="50" padding="16" src="{{this.icon}}" tint="{{color}}"/>
                    <text textColor="black" textSize="15sp" text="{{this.title}}" layout_gravity="center"/>
                </horizontal>
            </list>
        </vertical>
    </drawer>
);


//创建选项菜单(右上角)
// ui.emitter.on("create_options_menu", menu=>{
//     menu.add("设置");
//     menu.add("关于");
// });
//监听选项菜单点击
// ui.emitter.on("options_item_selected", (e, item)=>{
//     switch(item.getTitle()){
//         case "设置":
//             toast("还没有设置");
//             break;
//         case "关于":
//             alert("关于", "京东助理");
//             break;
//     }
//     e.consumed = true;
// });
activity.setSupportActionBar(ui.toolbar);

//设置滑动页面的标题
ui.viewpager.setTitles(["控制中心", "农场", "猪场"]);
//让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.viewpager);

//让工具栏左上角可以打开侧拉菜单
ui.toolbar.setupWithDrawer(ui.drawer);

ui.menu.setDataSource([
//   {
//       title: "选项一",
//       icon: "@drawable/ic_android_black_48dp"
//   },
//   {
//       title: "选项二",
//       icon: "@drawable/ic_settings_black_48dp"
//   },
  {
      title: "版本说明",
      icon: "@drawable/ic_assignment_black_48dp"
  },
  {
      title: "后台日志",
      icon: "@drawable/ic_bug_report_black_48dp"
  },
  {
      title: "退出",
      icon: "@drawable/ic_exit_to_app_black_48dp"
  }
]);

ui.menu.on("item_click", item => {
    switch(item.title){
        case "版本说明":
            openReleasePage();
            break;
        case "后台日志":
            app.startActivity("console");
            break;
        case "退出":
            ui.webView.destroy();
            ui.pigWebView.destroy();
            ui.finish();
            break;
    }
})


/** 业务逻辑开始 */
ui.exit.on("click", () => {
    ui.webView.destroy();
    ui.pigWebView.destroy();
    ui.finish();
});

ui.startFruit.on("click", () => {
    // 这里外部文本资源，必须以非js类型，否则，打包时会被加密，导致乱码
    webViewExpand.init(ui.webView, ["inject/vconsole.ts", "inject/shareWx.ts", "inject/tree.ts"]);
    let url = "https://h5.m.jd.com/babelDiy/Zeus/3KSjXqQabiTuD1cJ28QskrpWoBKT/index.html?babelChannel=45&collectionId=519&lng=113.39183&lat=23.13042&sid=ae12c4c53cdb18ded15e85ad8547e2aw&un_area=19_1601_3633_0";
    ui.webView.loadUrl(url);
});
ui.startPig.on("click", () => {
    webViewExpand.init(ui.pigWebView, ["inject/vconsole.ts", "inject/pig.ts"]);
    url = "https://uua.jr.jd.com/uc-fe-wxgrowing/cloudpig/index/home/20191030/158432123100870350/?channelLV=qdy&shareId=vZoq-O6FkuUp62HuKheuTcAdoUJQ3Dik&sourceID=240&actflag=1AEE706F1D&isPay=N&utm_source=Android%2aurl%2a1584321232229&utm_medium=jrappshare&utm_term=qqfriends";
    ui.pigWebView.loadUrl(url);
});
